f = fopen('log.txt','w');
sql = ['select bss.security_id,bss.`year`,bss.`quarter`,s.sector_id,s.short_name ' ...    
    'from security s, income_statement_stox iss, balance_sheet_stox bss '...    
    'where iss.security_id = bss.security_id '...
    'and iss.`quarter` = bss.`quarter` ' ...
    'and iss.`year` = bss.`year` '...
    'and iss.security_id=s.security_id'];

fids = get_info_from_database('sql',sql);

n = size(fids,1);

for i = 1 : n    
    fprintf('%d/%d - Calculating %s (%d), %d, %d...\n',i,n,fids{i,5},fids{i,1},fids{i,2},fids{i,3});
    fprintf(f,'%d/%d - Calculating %s (%d), %d, %d...\n',i,n,fids{i,5},fids{i,1},fids{i,2},fids{i,3});
    fi = financial_indicator('security',fids{i,1},fids{i,2},fids{i,3});
    if(isempty(fi))        
        continue
    end
    
    if(fids{i,4}~=2)&&(fids{i,4}~=6)&&(fids{i,4}~=12)
        sql = ['INSERT INTO `financial_indicators` VALUES (%d,%d,%d' ...
                repmat(',%f',1,13) ... 
                ')'];
    elseif (fids{i,4}==2) 
        sql = ['INSERT INTO `financial_indicators_insurance` VALUES (%d,%d,%d' ...
            repmat(',%f',1,12) ... 
            ')'];
    elseif (fids{i,4}==6)
        sql = ['INSERT INTO `financial_indicators_financial` VALUES (%d,%d,%d' ...
            repmat(',%f',1,11) ...
            ')'];
    elseif (fids{i,4}==12)
        continue
%         sql = ['INSERT INTO `financial_indicators_bank` VALUES (%d,%d,%d' ...
%             repmat(',%f',1,10) ...
%             ')'];
    end
    sql = sprintf(sql,fi);
    fprintf(f,'Importing ...');
    get_info_from_database('sql',sql);
    fprintf(f,'Done !\n');
end

sql = ['select bss.security_id,bss.`year`,bss.`quarter`,s.sector_id,s.short_name ' ...    
    'from security s, income_statement_bank_stox iss, balance_sheet_bank_stox bss '...    
    'where iss.security_id = bss.security_id '...
    'and iss.`quarter` = bss.`quarter` ' ...
    'and iss.`year` = bss.`year` '...
    'and iss.security_id=s.security_id'];

fids = get_info_from_database('sql',sql);

n = size(fids,1);

for i = 1 : n    
    fprintf('%d/%d - Calculating %s (%d), %d, %d...\n',i,n,fids{i,5},fids{i,1},fids{i,2},fids{i,3});
    fprintf(f,'%d/%d - Calculating %s (%d), %d, %d...\n',i,n,fids{i,5},fids{i,1},fids{i,2},fids{i,3});
    fi = financial_indicator('security',fids{i,1},fids{i,2},fids{i,3});
    if(isempty(fi))        
        continue
    end
    
    if(fids{i,4}==12)
        sql = ['INSERT INTO `financial_indicators_bank` VALUES (%d,%d,%d' ...
            repmat(',%f',1,10) ...
            ')'];
    end
    sql = sprintf(sql,fi);
    fprintf(f,'Importing ...');
    get_info_from_database('sql',sql);
    fprintf(f,'Done !\n');
end

fclose(f);